package com.huatai.bi.handler.datasource;

import com.huatai.bi.constant.SystemConstant;
import com.huatai.bi.entity.DatasourceEntity;
import com.huatai.bi.handler.DatasourceHandler;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;

@Log4j2
public class PostgreDatasource extends DatasourceHandler {

    //查询模式下的表
    String tableSql = "select t.table_catalog,t.table_schema,t.table_name,b.comments from ("
            +"select table_catalog,table_schema,table_name from information_schema.tables where table_schema=current_schema"
            +") t inner join ("
            +"select c.relname,obj_description(c.oid) as comments from pg_class c inner join pg_namespace n on c.relnamespace=n.oid and n.nspname = current_schema"
            +") b on t.table_name=b.relname ";

    //查询模式下的表字段
    String columnSql = "select c.table_name,c.column_name,b.comment,b.type,c.ordinal_position,c.column_default,c.is_nullable from ("
            +"select * from information_schema.columns where table_schema=current_schema ) c inner join ("
            +"select c1.relname,a.attname,col_description(a.attrelid,a.attnum) as comment,format_type(a.atttypid,a.atttypmod) as type from pg_class c1"
            +" inner join pg_attribute a on c1.oid = a.attrelid"
            +" inner join pg_namespace n on c1.relnamespace=n.oid and n.nspname = current_schema and a.attnum>0"
            +") b on c.table_name=b.relname and c.column_name = b.attname";
    //数据库版本号
    String versionSql = "show server_version";
    //模式
    String schemaSql = "SELECT schema_name FROM information_schema.schemata";

    public PostgreDatasource(DatasourceEntity entity){
        super(entity);
    }

    @Override
    public String jdbcUrl(DatasourceEntity entity) {
        StringBuffer url = new StringBuffer(entity.getJdbcUrl());

        //url拼接模式和编码
        String schema = entity.getSchemaName();
        if(StringUtils.isNoneBlank(schema) && !"public".equals(schema)){
            if(url.indexOf("?")<0){
                url.append("?");
            }else{
                url.append("&");
            }
            url.append("currentSchema=").append(schema);
        }
        if(StringUtils.isNoneBlank(entity.getEncode()) && url.indexOf("characterEncoding") < 0){
            if(url.indexOf("?")<0){
                url.append("?");
            }else{
                url.append("&");
            }
            url.append("characterEncoding=").append(entity.getEncode());
        }
        return url.toString();
    }

    @Override
    public void sqlInit(){
        switch (type){
            case SystemConstant.JDBC_SQL_TYPE_TABLE:
                sql = tableSql;
                return;
            case SystemConstant.JDBC_SQL_TYPE_COLUMN:
                sql = columnSql;
                return;
            case SystemConstant.JDBC_SQL_TYPE_VERSION:
                sql = versionSql;
                return;
            case SystemConstant.JDBC_SQL_TYPE_SCHEMA:
                sql = schemaSql;
                return;
            case SystemConstant.JDBC_SQL_TYPE_SQL_PAGE:

                sql = "select t.* from ("+sql+") t limit "+pageSize+" offset "+pageStartIndex;
                return;
            case SystemConstant.JDBC_SQL_TYPE_SQL_PAGE_COUNT:
                sql = "select count(1) from ("+sql+") t";
                return;
            default:return;
        }
    }
}
